<?php
class Controller_Admin_Track extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_no_buttons = TRUE;
        $this->_list_pagesize = 10;
        $this->_form_type  = 'track';

        parent::__construct($req, $res);

        $this->_list_where = array(
            array('status', '=', 'VALID'),
        );
    }

    public function action_index ()
    {
        $this->action_list();
    }

    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }

        parent::action_create();
    }

    public function action_update ($nid)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }
        else
        {
            $data = Form_Data::detail($this->_form_type, $nid); 
            $this->_form_data = $data;
        }

        parent::action_update($nid);
    }

    public function action_detail ($nid)
    {
        $data = Form_Data::detail($this->_form_type, $nid); 

        $this->_form_data = $data;
        parent::action_detail($nid);
    }

    public function action_category ($nid)
    {
    	$post = $this->request->post();
    	if (empty($post))
    	{
    		$this->template->set_filename('smarty:www/track/admin_category');
    
    		$cates = App_Category::all_categories(0, 0, FALSE, 3);
    		$this->template->set('cates', $cates);
    
    		$track   = new App_Track($nid);
    		$detail= $track->detail();
    		$this->template->set('track', $detail);
    
    		$relation = App_CategoryGL::listByTarget('TRACK', $nid);
    		$catIds   = array();
    		foreach ($relation as $rs)
    		{
    			$catIds[] = $rs['categoryId'];
    		}
    
    		$this->template->set('catIds', $catIds);
    	}
    	else
    	{
    		$cids = Arr::get($post, 'cid');
    		App_CategoryGL::setTarget(user::current_user_id(), 'TRACK', $nid, $cids);
    		$this->to_url('list');
    	}
    }

    private function rebuild_post_data (& $post)
    {
        //
        $post['managerUid'] = user::current_user_id();

    }

    public function list_get_data ($offset, $limit)
    {
        $track = new App_Track; 
        list($count, $tracks) = $track->search(NULL, $offset, $limit);

        $this->_list_total_size = $count;
        return $tracks;
    }

    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
            $rs['guiderName'] = $rs['guider']['realName'];
            $price = $rs['priceTotal'];
            $rs['price'] = number_format($price/100, 2) . ' €/天';
        }
    }


    public function classname_by_list_row (& $row)
    {
    }

    public function actions_by_list_row (& $row, & $menus)
    {
	    $controller = Request::initial()->controller();
	    $directory  = Request::initial()->directory();
	    $directory  = empty($directory) ? '' : '/'.$directory;

        unset($menus[1]);
        unset($menus[0]);

        array_unshift($menus, '<a target="_blank" href="/track/detail/'.$row['id'].'">查看</a>');
        $menus[] = '<a href="/admin/track/category/'.$row['id'].'">分类</a>';
     }

    public function action_delete ($nid)
    {
        $track = new App_Track($nid);
        $track->remove();
        $this->to_url('list');
    }

}


